package com.itheima.controller;

import com.itheima.common.PageBean;
import com.itheima.common.R;
import com.itheima.dto.ClazzDto;
import com.itheima.pojo.Clazz;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
@Slf4j
public class ClazzController {
    @Autowired
    ClazzService clazzService;

    @GetMapping
    public R pageGet(ClazzDto dto) {
        log.info("分页查询：{}", dto);
        PageBean bean = clazzService.pageGet(dto);
        return R.ok(bean);
    }

    @PostMapping
    public R insert(@RequestBody Clazz clazz) {
        log.info("添加班级：{}", clazz);
        clazzService.insert(clazz);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        log.info("根据id查询班级：{}", id);
        Clazz clazz = clazzService.getById(id);
        return  R.ok(clazz);
    }
    @PutMapping
    public R  updateById(@RequestBody Clazz clazz){
        log.info("修改班级：{}",clazz);
        clazzService.updateById(clazz);
        return R.ok();
    }
    @GetMapping("/list")
    public R getAll(){
        log.info("查询所有班级");
      List<Clazz> clazzList =  clazzService.getAll();
      return R.ok(clazzList);
    }
    @DeleteMapping("/{id}")
    public  R deleteById(@PathVariable Integer id){
        log.info("删除班级");
        clazzService.deleteById(id);
        return R.ok();
    }

}
